<template>
	<view class="box">
		<!-- 导航 -->
		<uv-navbar title="发布动态" bgColor="#65c9bb" height="44px" titleStyle="{font-weight: bold;font-size: 1.1rem;}"
			@click="goback"></uv-navbar>
		<view class="contact">
			<textarea placeholder="请输入动态内容发布" name="" id="" cols="30" rows="10"></textarea>
			<uv-upload
					:fileList="fileList1"
					name="1"
					multiple 
					:maxCount="10"
					@afterRead="afterRead" 
					@delete="deletePic" 
				></uv-upload>
		</view>
		<!-- 按钮 -->
		<view class="btn">
			<uv-button type="primary" color="#008c8c" shape="circle" text="新增地址"></uv-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				fileList1: []

			}
		},
		methods: {
			goback() {
				uni.navigateBack({
					delta: 1
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			}
		}
	}
</script>

<style lang="scss">
	@import url("released.scss");
</style>